package org.makabaka.wfwwb.controller;


import org.apache.ibatis.annotations.Mapper;
import org.makabaka.wfwwb.mapper.*;
import org.makabaka.wfwwb.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/select")
public class SelectController {
    @Autowired
    private TeamMapper teamMapper;

    @Autowired
    private TeamMemberMapper teamMemberMapper;

    @Autowired
    private AProblemMapper aProblemMapper;

    @Autowired
    private ProblemSelectionMapper problemSelectionMapper;

    @Autowired
    private JudgeMapper judgeMapper;

    @Autowired
    private AScoreMapper aScoreMapper;

    @Autowired
    private BScoreMapper bScoreMapper;

    // 查询所有团队信息
    @GetMapping("/teams")
    public Result getAllTeams() {
        try {
            List<Team> teams = teamMapper.selectList(null);
            if (teams.isEmpty()) {
                return Result.error("500", "未查询到任何信息");
            }
            return Result.success(teams);
        } catch (Exception e) {
            return Result.error("500", "查询团队信息失败: " + e.getMessage());
        }
    }

    // 查询所有团队成员信息
    @GetMapping("/team-members")
    public Result getAllTeamMembers() {
        try {
            List<TeamMember> teamMembers = teamMemberMapper.selectList(null);
            if (teamMembers.isEmpty()) {
                return Result.error("500", "未查询到任何信息");
            }
            return Result.success(teamMembers);
        } catch (Exception e) {
            return Result.error("500", "查询团队成员信息失败: " + e.getMessage());
        }
    }

    // 查询所有 A 类题目信息
    @GetMapping("/a-problems")
    public Result getAllAProblems() {
        try {
            List<AProblem> aProblems = aProblemMapper.selectList(null);
            if (aProblems.isEmpty()) {
                return Result.error("500", "未查询到任何信息");
            }
            return Result.success(aProblems);
        } catch (Exception e) {
            return Result.error("500", "查询 A 类题目信息失败: " + e.getMessage());
        }
    }

    // 查询所有团队选题信息
    @GetMapping("/problem-selections")
    public Result getAllProblemSelections() {
        try {
            List<ProblemSelection> problemSelections = problemSelectionMapper.selectList(null);
            if (problemSelections.isEmpty()) {
                return Result.error("500", "未查询到任何信息");
            }
            return Result.success(problemSelections);
        } catch (Exception e) {
            return Result.error("500", "查询团队选题信息失败: " + e.getMessage());
        }
    }

    // 查询所有裁判信息
    @GetMapping("/judges")
    public Result getAllJudges() {
        try {
            List<Judge> judges = judgeMapper.selectList(null);
            if (judges.isEmpty()) {
                return Result.error("500", "未查询到任何信息");
            }
            return Result.success(judges);
        } catch (Exception e) {
            return Result.error("500", "查询裁判信息失败: " + e.getMessage());
        }
    }

    // 查询所有 A 类评分信息
    @GetMapping("/a-scores")
    public Result getAllAScores() {
        try {
            List<AScore> aScores = aScoreMapper.selectList(null);
            if (aScores.isEmpty()) {
                return Result.error("500", "未查询到任何信息");
            }
            return Result.success(aScores);
        } catch (Exception e) {
            return Result.error("500", "查询 A 类评分信息失败: " + e.getMessage());
        }
    }

    // 查询所有 B 类评分信息
    @GetMapping("/b-scores")
    public Result getAllBScores() {
        try {
            List<BScore> bScores = bScoreMapper.selectList(null);
            if (bScores.isEmpty()) {
                return Result.error("500", "未查询到任何信息");
            }
            return Result.success(bScores);
        } catch (Exception e) {
            return Result.error("500", "查询 B 类评分信息失败: " + e.getMessage());
        }
    }

    // 查询团队信息
    @GetMapping("/team/info/{id}")
    public Result getTeamInfo(@PathVariable("id") Integer teamId) {
        Team team = teamMapper.selectById(teamId);
        if (team != null) {
            return Result.success(team);
        } else {
            return Result.error("404", "团队信息未找到");
        }
    }

    // 查询团队成员信息
    @GetMapping("/team/members/{id}")
    public Result getTeamMembers(@PathVariable("id") Integer teamId) {
        List<TeamMember> members = teamMemberMapper.selectByTeamId(teamId);
        if (members != null && !members.isEmpty()) {
            return Result.success(members);
        } else {
            return Result.error("404", "团队成员信息未找到");
        }
    }

    // 查询选题信息
    @GetMapping("/team/problems/{id}")
    public Result getProblemSelections(@PathVariable("id") Integer teamId) {
        List<ProblemSelection> problems = problemSelectionMapper.selectByTeamId(teamId);
        if (problems != null && !problems.isEmpty()) {
            return Result.success(problems);
        } else {
            return Result.error("404", "选题信息未找到");
        }
    }

    // 查询最终得分
    @GetMapping("/team/final-score/{id}")
    public Result getFinalScore(@PathVariable("id") Integer teamId) {
        // 1. 根据团队 ID 查询赛道类型
        String trackType = null;
        try {
            trackType = problemSelectionMapper.getTrackTypeByTeamId(teamId);
        } catch (Exception e) {
            return Result.error("500", "未查询到该团队赛道类型");
        }

        // 2. 根据赛道类型查询相应的分数

        if ("A".equals(trackType)) {
            try {
                List<AScore> aScores = aScoreMapper.getAScoreByTeamId(teamId);
                return Result.success(aScores);
            } catch (Exception e) {
                return Result.error("500", "未查询到该团队成绩");
            }
        } else if ("B".equals(trackType)) {
            try {

                List<BScore> bScores = bScoreMapper.getBScoreByTeamId(teamId);
                return Result.success(bScores);
            } catch (Exception e) {
                return Result.error("500", "未查询到该团队成绩");
            }
        } else {
            return Result.error("500", "非法赛道类型"); // 或者抛出异常，表示未找到对应赛道类型
        }


    }


//    // 查询获奖状态
//    @GetMapping("/team/award-status/{id}")
//    public String getAwardStatus(@PathVariable("id") Integer teamId) {
//        return awardService.checkAwardStatus(teamId);
//    }
}
